#include "record_diagnosis.h"

#include <algorithm>
#include <future>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "base_file_util.h"
#include "base_utils.h"
#include "base_string.h"
#include "base_log.h"
#include "common_shell.h"

#include "record_record.h"
#include "record_snap.h"
#include "record_search.h"

namespace El {
namespace Record {

RecordDiagnosis &RecordDiagnosis::GetInstance()
{
    static RecordDiagnosis instance;
    return instance;
}

bool RecordDiagnosis::Start()
{
    Common::Shell::GetInstance().RegisterCommand("record",
                                                 std::bind(&RecordDiagnosis::Record, this, std::placeholders::_1));

    Common::Shell::GetInstance().RegisterCommand("snap",
                                                 std::bind(&RecordDiagnosis::Snap, this, std::placeholders::_1));

    Common::Shell::GetInstance().RegisterCommand("search",
                                                 std::bind(&RecordDiagnosis::Search, this, std::placeholders::_1));
    return true;
}

void RecordDiagnosis::Record(const std::string &args)
{
    std::vector<std::string> vec_args;
    Base::Split(args, " ", vec_args);
    if (vec_args.size() < 1) {
        LOG_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    std::string text = "";
    if (vec_args[0] == "start") {
        LOG_INFO("开始手动录像");
        IRecord::GetInstance().StartManual(0);
    } else if (vec_args[0] == "stop") {
        LOG_INFO("停止手动录像");
        IRecord::GetInstance().StopManual(0);
    }
}

void RecordDiagnosis::Snap(const std::string &args)
{
    std::vector<std::string> vec_args;
    Base::Split(args, " ", vec_args);
    if (vec_args.size() < 1) {
        LOG_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    std::string text = "";
    if (vec_args[0] == "start") {
        LOG_INFO("开始手动抓图");
        ISnapStorage::GetInstance().manualSnap(0);
    }
}

void RecordDiagnosis::Search(const std::string &args)
{
    std::vector<std::string> vec_args;
    Base::Split(args, " ", vec_args);
    if (vec_args.size() < 1) {
        LOG_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    std::string text = "";
    if (vec_args[0] == "video") {
        LOG_INFO("开始搜索录像");
        El::Record::TimePoint start = std::chrono::system_clock::from_time_t(0);
        // 设置结束时间为2038年1月1日 00:00:00 UTC
        El::Record::TimePoint end = std::chrono::system_clock::from_time_t(2145916800);

        RecordSearchPtr search = IRecordSearch::create();
        search->searchVideo(0, start, end);

        int32_t found = 0;
        std::vector<VideoFileInfo> fileList;
        search->findNextVideo(100000, found, fileList);

        for (const auto &file : fileList) {
            LOG_INFO("Found video file: {}", file.fileName.c_str());
        }
    } else if (vec_args[0] == "snap") {
        LOG_INFO("开始搜索抓图");
        El::Record::TimePoint start = std::chrono::system_clock::from_time_t(0);
        // 设置结束时间为2038年1月1日 00:00:00 UTC
        El::Record::TimePoint end = std::chrono::system_clock::from_time_t(2145916800);
        RecordSearchPtr search = IRecordSearch::create();
        search->searchImage(0, start, end);

        int32_t found = 0;
        std::vector<ImageFileInfo> fileList;
        search->findNextImage(100000, found, fileList);

        for (const auto &file : fileList) {
            LOG_INFO("Found image file: {}", file.fileName.c_str());
        }
    }
}

} // namespace Record
} // namespace El